package kind

import (
	"encoding/json"
	"strconv"

	"github.com/labstack/echo"
	"gopkg.in/mgo.v2/bson"

	"oxy_server/model"
	"oxy_server/module/log"
)

type (
	newproductkind struct {
		ID     bson.ObjectId `json:"_id" bson:"_id"`
		Name   string        `json:"productkindname" bson:"productkindname"`
		Detail []detail      `json:"detail" bson:"detail"`
	}
	newinsertproductkind struct {
		Name   string   `json:"productkindname" bson:"productkindname"`
		Detail []detail `json:"detail" bson:"detail"`
	}
	detail struct {
		Brand string `json:"brand" bson:"brand"` //品牌
		Code  string `json:"code" bson:"code"`
	}
)
type OxyNewProductKindData struct{}

func NewOXYProductData() *OxyNewProductKindData {
	Data := &OxyNewProductKindData{}
	return Data
}
func (pk *OxyNewProductKindData) GetProductKindListHandler(c echo.Context) error {
	currentpage, err := strconv.Atoi(c.QueryParam("currentpage"))
	perpage, err := strconv.Atoi(c.QueryParam("perpage"))
	if err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	model.DB().Dbname = "OxyDic"
	model.DB().Document = "newproduct_kind_dic"
	query, count, err := model.DB().Query(nil, currentpage, perpage)
	if err != nil {
		log.Error("查询出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	ps := []newproductkind{}
	query.All(&ps)

	str, err := json.Marshal(ps)
	if err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg":   "success",
		"data":  string(str),
		"count": count,
	})

	return nil

}
func (pk *OxyNewProductKindData) AddProductKindHandler(c echo.Context) error {
	p := &newinsertproductkind{}
	if err := c.Bind(p); err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	model.DB().Dbname = "OxyDic"
	model.DB().Document = "newproduct_kind_dic"
	err := model.DB().Insert(p)
	if err != nil {
		log.Error("插入出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})

	return nil

}
func (pk *OxyNewProductKindData) UpdateProductKindHandler(c echo.Context) error {
	id := c.QueryParam("id")
	log.Info("UpdateProductKindHandler", id)
	p := &newinsertproductkind{}
	if err := c.Bind(p); err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	model.DB().Dbname = "OxyDic"
	model.DB().Document = "newproduct_kind_dic"
	err := model.DB().Update(bson.M{"_id": bson.ObjectIdHex(id)}, p)
	if err != nil {
		log.Error("插入出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})

	return nil
}
func (pk *OxyNewProductKindData) DelProductKindHandler(c echo.Context) error {
	id := c.QueryParam("id")
	log.Info("DelProductKindHandler", id)
	model.DB().Dbname = "OxyDic"
	model.DB().Document = "newproduct_kind_dic"
	err := model.DB().Remove(bson.M{"_id": bson.ObjectIdHex(id)})
	if err != nil {
		log.Error("删除出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})

	return nil
}
